package site.kexing.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import site.kexing.config.MybaitsUtils;
import site.kexing.mapper.EquipmentMapper;
import site.kexing.mapper.Product_PlanMapper;
import site.kexing.mapper.Product_ScheduleMapper;
import site.kexing.pojo.Product_Schedule;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class Product_ScheduleController {
    @Autowired
    private Product_PlanMapper product_planMapper;
    @Autowired
    private Product_ScheduleMapper product_scheduleMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping("/selectallproductschedule")
    public String selectAllProduct_Schedule(@RequestParam(defaultValue = "1") Integer currentPage, Model model){
        PageHelper.startPage(currentPage, 7);
        List<Product_Schedule> product_schedules = product_scheduleMapper.selectAllProduct_Schedule();
        PageInfo<Product_Schedule> pageInfo = new PageInfo<Product_Schedule>(product_schedules,7);
        model.addAttribute("pageInfo",pageInfo);
        return "product_schedule";
    }

    @RequestMapping("/selectproductschedulebyid")
    public String selectProduct_ScheduleByID(Integer id, Model model){
        Product_Schedule product_schedule = product_scheduleMapper.selectProduct_ScheduleByID(id);
        List<Integer> eids = equipmentMapper.selectAllEquipmentID();
        model.addAttribute("product_schedule",product_schedule);
        model.addAttribute("eids",eids);
        return "product_scheduleUpdate";
    }

    @RequestMapping("/updateproductschedule")
    public String updateProduct_Schedule(Product_Schedule product_schedule){
        product_schedule.setUpdate_time(new Date());
        product_scheduleMapper.updateProduct_Schedule(product_schedule);
        return "redirect:/selectallproductschedule";
    }

    @RequestMapping("/deleteproductschedule")
    public String deleteProduct_Schedule(Integer id){
        product_scheduleMapper.deleteProduct_Schedule(id);
        return "redirect:/selectallproductschedule";
    }

    @RequestMapping("/toproductscheduleinsert")
    public String toProduct_ScheduleInsert(Model model){
        List<Integer> eids = equipmentMapper.selectAllEquipmentID();
        List<Integer> product_planIDs = product_planMapper.selectAllProduct_PlanID();
        model.addAttribute("product_planIDs",product_planIDs);
        model.addAttribute("eids",eids);
        return "product_schedualInsert";
    }

    @RequestMapping("/insertproductschedule")
    public String insertProduct_Schedule(Product_Schedule product_schedule,Model model){
        product_schedule.setCreate_time(new Date());
        product_schedule.setUpdate_time(new Date());
        product_schedule.setSchedule_seq(MybaitsUtils.getUUID());
        product_scheduleMapper.insertProduct_Schedule(product_schedule);
        return "redirect:/selectallproductschedule";
    }

    @RequestMapping("/selectProductschedulebyplanid")
    public String selectProduct_ScheduleByPlanID(@RequestParam(defaultValue = "1") Integer currentPage,String plan_id,Model model){
        PageHelper.startPage(currentPage, 7);
        Integer id = 0;
        if(plan_id==null || plan_id==""){
            id = null;
        }else{
            id = Integer.parseInt(plan_id);
        }
        List<Product_Schedule> product_schedules = product_scheduleMapper.selectProduct_ScheduleByPlanID(id);
        PageInfo<Product_Schedule> pageInfo = new PageInfo<Product_Schedule>(product_schedules,7);
        model.addAttribute("pageInfo",pageInfo);
        return "product_schedule";
    }
}
